package net.piggy.core.config;

import net.piggy.core.model.HttpStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class GatewayConfig {

    private String zookeeperAddress;
    private String hostName = "127.0.0.1";
    private int port = 7397;
    private int bossNThreads = 1;
    private int workNThreads = 8;

    public int getBossNThreads() {
        return bossNThreads;
    }

    public void setBossNThreads(int bossNThreads) {
        this.bossNThreads = bossNThreads;
    }

    public int getWorkNThreads() {
        return workNThreads;
    }

    public void setWorkNThreads(int workNThreads) {
        this.workNThreads = workNThreads;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getZookeeperAddress() {
        return zookeeperAddress;
    }

    public void setZookeeperAddress(String zookeeperAddress) {
        this.zookeeperAddress = zookeeperAddress;
    }

    private final Map<String, HttpStatement> statementMap = new ConcurrentHashMap<>();

    private final Map<String, List<String>> applicationMap = new ConcurrentHashMap<>();


    public void registryConfig(String application, Map<String ,HttpStatement> map){
        Set<Map.Entry<String, HttpStatement>> entries = map.entrySet();
        List<String> list = new ArrayList<>();
        for(Map.Entry<String, HttpStatement> entry : entries){
            String key = entry.getKey();
            list.add(key);
            HttpStatement value = entry.getValue();
            putHttpStatement(key, value);
        }
        putApplication(application, list);
    }

    public void putApplication(String name, List<String> uris){
        applicationMap.put(name, uris);
    }
    public void removeApplication(String name){
        List<String> list = applicationMap.remove(name);
        batchRemoveHttpStatement(list);
    }
    public void batchRemoveHttpStatement(List<String> uris){
        for(String uri : uris){
            removeHttpStatement(uri);
        }
    }
    public void putHttpStatement(String uri, HttpStatement httpStatement){
        statementMap.put(uri, httpStatement);
    }
    public void removeHttpStatement(String uri){
        statementMap.remove(uri);
    }

    public HttpStatement getHttpStatement(String uri){
        return statementMap.get(uri);
    }
    public List<String> getApplicationUris(String uri){
        return applicationMap.get(uri);
    }
}
